import { useTable, LIST_TYPE } from '@crud/index';
import { getOrderList as getList } from '@/api/crm-kgj/order';
import { toolOptions2Map, formatParamsSelectVal } from '@/utils';

export const tradeStatusOptions = [
  { label: '不限', value: -1 },
  { label: '已付款', value: 200 },
  { label: '已退款', value: -300 }
];
export const payListOptions = [
  { value: -1, label: '不限' },
  { value: 1, label: '微信' },
  { value: 2, label: '支付宝' },
  { value: 3, label: '苹果内购' },
  { value: 4, label: '对公转账' },
  { value: 5, label: '渠道支付' },
  { value: 6, label: '渠道key支付' },
  { value: 7, label: '抖店' },
  { value: 8, label: '花呗分期' }
];
/** 是否开票 */
export const invoiceStatusOptions = [
  { label: '不限', value: -1 },
  { label: '已开票', value: 2 },
  { label: '开票中', value: 1 },
  { label: '未开票', value: 0 }
];
/** 产品类型枚举 */
export const productTypeOptions = [
  { label: '会员购买', value: 1 },
  { label: '会员续费', value: 2 },
  { label: '团队扩充', value: 3 }
];
/** 会员类型枚举 */
export const levelOptions = [
  { label: '至尊版', value: 30 },
  { label: '旗舰版', value: 20 },
  { label: '轻享版', value: 10 },
  { label: '体验版', value: 0 }
];
/** 优惠类型枚举 */
export const discountTypeOptions = [
  { label: '价格折扣', value: 1 },
  { label: '赠送时长', value: 2 }
];
const tradeStatusMap = toolOptions2Map(tradeStatusOptions);
const invoiceStatusMap = toolOptions2Map(invoiceStatusOptions);
export const useTableList = (options: any = {}) => {
  const columns = [
    {
      label: '流水单号',
      prop: 'trade_no',
      align: 'center',
      showOverflowTooltip: true,
      type: 'copy',
      minWidth: 250
    },
    {
      label: '用户名称',
      prop: 'nick_name',
      width: 140,
      align: 'center',
      render: 'nick_name'
    },
    {
      label: '购买类型',
      prop: 'subject',
      align: 'center',
      width: 180
    },
    {
      label: '支付金额',
      prop: 'total_fee',
      align: 'center',
      type: 'priceUnit',
      width: 120
    },
    {
      label: '充值方式',
      prop: 'terminal_type',
      align: 'center',
      width: 130
    },
    {
      label: '支付方式',
      prop: 'trade_type',
      align: 'center',
      width: 130
    },
    {
      label: '订单时间',
      prop: 'start_time',
      type: 'dateTime',
      align: 'center',
      width: 177
    },
    {
      label: '支付时间',
      prop: 'complete_time',
      type: 'dateTime',
      align: 'center',
      width: 177
    },
    {
      label: '订单备注',
      prop: 'remark',
      align: 'center',
      showOverflowTooltip: true,
      minWidth: 200
    },
    {
      label: '开票状态',
      prop: 'invoice_status',
      align: 'center',
      type: 'boolean',
      render: (record) => invoiceStatusMap[record.row.invoice_status] ?? '-',
      width: 100
    },
    {
      label: '订单状态',
      prop: 'trade_status',
      align: 'center',
      render: (record) => tradeStatusMap[record.row.trade_status] ?? '-',
      width: 100
    },
    {
      label: '操作',
      align: 'center',
      render: 'actions',
      width: 160,
      fixed: 'right'
    }
  ];

  return useTable({
    ...options,
    type: LIST_TYPE.KGJ,
    params: {
      trade_type: -1,
      trade_status: 200,
      invoice_status: -1
    },
    getParams: ({ _timeArr = [], ...params }) => {
      // 处理时间参数
      if (_timeArr?.length) {
        const [begin, end] = _timeArr;
        params.begin = begin;
        params.end = end;
      }

      return {
        ...params,
        ...formatParamsSelectVal(
          [
            ['trade_type', -1],
            ['trade_status', -1],
            ['invoice_status', -1]
          ],
          params
        )
      };
    },
    columns,
    getList
  });
};
